<?php
// +----------------------------------------------------------------------
// | 通用PHP框架 [ ThinkPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 郑州智众 [Paulxun@qq.com]
// +----------------------------------------------------------------------
// | 官方网站: https://www.zzmind.com
// +----------------------------------------------------------------------

namespace app\mall\home;

use app\index\controller\Home;
use app\mall\model\Cart as CartModel;
use app\mall\model\Category as CategoryModel;
use app\mall\model\Goods as GoodsModel;;
use app\user\model\Member as MemberModel;
use app\user\model\Vip as VipModel;
use util\Wxsdk;
use baidu\Speech;
use think\facade\Db;
/**
 * 购物车
 * @package app\mall\home
 */
class Cart extends Home
{
    /**
     * 跳转到第一个有权限访问的url
     * @author PaulXun <paulxun@qq.com>
     * @return mixed|string
     */
    public function index()
    {
		$category = CategoryModel::getList();
		$this->assign('list', $category);
		
		if (request()->isAjax()){
			$uid = is_login();
			$goods_ids = GoodsModel::where('status', '=', 1)->column('id');
			$map = [
				['mall_cart.uid', '=', $uid],
				['mall_cart.gid', 'in', $goods_ids],
			];
			$order = 'mall_cart.update_time desc';
			$data_list = CartModel::getAll($map, $order);
			if ($data_list->isEmpty()) {
				$this->error('购物车是空的');
			} else {
				foreach ($data_list as $key => $value) {
					$data_list[$key]['cover'] = get_file_path($value['logo']);
					$data_list[$key]['price'] = $value['price'] / 100;
					$data_list[$key]['pay_price'] = $value['pay_price'] / 100;
				}
				$this->success('加载成功', '', $data_list);
			}
		}
		
        //重新定义active
        $this->view->active = 'cart';
		// 渲染模板
		return $this->fetch();
    }
	/**
	 * 添加购物车
	 * param int $gid 商品ID
	 * param int $nums 数量
	 * return int 购物车商品数
	 **/
	public function add()
	{
		if (request()->isPost()){
			$param = request()->post();
			if(!isset($param['gid']) || !isset($param['nums'])) $this->error('参数错误！');
			$gid = $param['gid'];
			$nums = $param['nums'];
			if (empty($param['remark'])) $param['remark'] = '';
			if ($nums == 0) $this->error('数量不正确');
			
			
			$uid = is_login();
			// 查找购物车信息
			$cart = CartModel::where('gid', $gid)->where('uid', $uid)->find();
			
			if ($cart){
				$nums = $nums + $cart['number'];
				if ($nums <= 0){
					$cart->delete();
					$result = [
						'cart' 	=> CartModel::where('uid', $uid)->count('id'),
					];
					$this->success('清除成功', '', $result);
				} 
			}
			
			// 查找会员信息
			$vip = VipModel::where('member_id', '=', $uid)->where('status', '=', 1)->find();
			if (empty($vip)) {
				$this->error('vip资料错误');
			}
			if ($vip['group'] <= 0) $this->error('会员等级不足');
			// 查找商品信息
			$goods = GoodsModel::where('id', $gid)->where('status', 1)->find();
			if (!$goods) $this->error('该商品不能购买');
			
			$category = CategoryModel::where('status', '=', 1)->where('id', '=', $goods['cid'])->where('auth', 'find in set', $vip['shop'])->find();
			
			if (!$category) $this->error('商品购买权限不足');
			
			// 获取库存
			$goods['nums'] = $goods['number'];
			if (!$cart){
				$remark = '';
				$data['number'] = $nums;
			} else {
				$data['id'] = $cart['id'];
				$remark = $cart['remark'];
				$data['number'] = $nums;
			}
			// 商品数量
			if($goods['nums'] < $data['number']) $this->error('商品库存不足');
			
			// 判断备注信息
			if ($remark == '' && $goods['beremark'] == 1 && $param['remark'] == ''){
				$this->error('请备注信息');
			}
			// 备注信息
			if ($param['remark'] != '') $data['remark'] = $param['remark'];
			
			// 获取用户已购买数量
			// $payed = OrderModel::where('uid', $uid)->where('gid', $goods['id'])->where('status', '<', 5)->sum('number');
			// if($payed + $data['number'] > $goods['canbuy'] && $goods['canbuy'] > 0) $this->error('你的购买数量已经超过限制，如有未支付订单请先取消');
			$data['gid'] = $gid;
			$data['uid'] = $uid;
			$data['pay_price'] = $goods['price'] * $data['number'];
			if (!$cart){
				$return = CartModel::create($data);
			} else {
				$return = CartModel::update($data);
			}
			if (!$return) $this->error('添加购物车失败');
			$result = [
				'cart' 	=> CartModel::where('uid', $uid)->count('id'),
			];
			$this->success('添加成功', '', $result);
		}
	    $this->success('调取成功');
	}
	/**
	 * 删除购物车
	 * param int $ids 商品购物车ID数组
	 **/
	public function delete()
	{
		if (request()->isPost()){
			$param = request()->post();
			if(!isset($param['ids'])) $this->error('参数错误！');
			$ids = is_array($param['ids']) ? $param['ids'] : (array)$param['ids'];
			$uid = is_login();
			// 购物车信息
			$cart = CartModel::where('id', 'in', $ids)->where('uid', '=', $uid)->delete();
			$this->success('删除成功');
		}
	    $this->success('调取成功');
	}
}
